﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvvmFoundation.Wpf;
using System.Windows.Controls;
using Revis.VClinicNet.UI.External.Client.Service;
using System.ComponentModel;

namespace Revis.VClinicNet.UI.External.Client.ViewModel
{

    public interface IWorkspaceControl
    {
        string DisplayName { get; }
        UserControl Workspace { get; }
        void Initialize(string displayName, IPatientService ips);
        void Run();
        void ChangeState(string status, RequestState state);
        event EventHandler<WorkspaceStateChangeEventArgs> OnStateChanged;
    }

    public enum RequestState
    {
        Requesting, Success, Failed, Faulted
    }

    public class WorkspaceStateChangeEventArgs : EventArgs
    {
        public WorkspaceStateChangeEventArgs(string status, RequestState state)
        {
            Status = status;
            State = state;
        }

        public RequestState State { get; private set; }
        public string Status { get; private set; }
    }

    public abstract class WorkspaceControlViewModelBase<TView> : ObservableObject, IWorkspaceControl
        where TView : UserControl, new()
    {
        protected IPatientService _patientService = null;

        public event EventHandler<WorkspaceStateChangeEventArgs> OnStateChanged = delegate { };

        public WorkspaceControlViewModelBase()
            : this(String.Empty, null)
        {
        }

        public WorkspaceControlViewModelBase(string displayName, IPatientService ips)
        {
            DisplayName = displayName;
            _patientService = ips;
        }

        public abstract void Run();

        public void Initialize(string displayName, IPatientService ips)
        {
            DisplayName = displayName;
            _patientService = ips;
        }

        public void ChangeState(string status, RequestState state)
        {
            OnStateChanged(this, new WorkspaceStateChangeEventArgs(status, state));
        }

        private UserControl _workspace = null;
        public UserControl Workspace
        {
            get
            {
                _workspace = _workspace ?? new TView() { DataContext = this };
                return _workspace;
            }
        }

        private string _displayName = String.Empty;
        public string DisplayName
        {
            get { return _displayName; }
            set { _displayName = value; }
        }
    }
}
